package ar.com.altaagenda;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import ar.com.altaagenda.spiders.CVSpider;
import ar.com.altaagenda.spiders.VASpider;


public class Chorizo {
	
	private static int DAYS_TO_CRAWL = 7;

	public static void main(String[] args){
		Calendar startDate = Calendar.getInstance();
		Calendar endDate = Calendar.getInstance();
		endDate.add(Calendar.DAY_OF_YEAR, DAYS_TO_CRAWL);
		
		try {
			List<Result> results = new ArrayList<Result>();

			results.addAll(new VASpider(startDate, endDate).getResults());
			results.addAll(new CVSpider(startDate, endDate).getResults());
			
			int resultCount = writeResults(results);
			
			System.out.println("Se escribieron "+resultCount+" resultados satisfactoriamente");
		} catch (Exception ex){
			ex.printStackTrace();
		}
	}	
	
	private static int writeResults(List<Result> results) throws Exception{
		int resultCount = 0;
		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("resultados.txt"),"UTF-8");
		
		for (Result result : results) {
			out.write(result.toString());
			resultCount++;
		}
		
		out.close();
		return resultCount;
	}
}